package com.hitrust.gs.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductTypeManagerBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSProductTypeManagerDao;
import com.hitrust.gs.dao.GSProviderDAO;
import com.hitrust.gs.form.GSImportExcelForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSProductTypeView;

public class GSExcelToProductType extends GSBaseAction{

    public static final String SHEET_NAME = "产品供应商分类信息";
    public static final String SEP = "%";
    public static String[] HEAD_NAMES = { "序号", "集中编码","产品供应商分类" ,"备注"};
    public static String[] HEAD_NAMES_PRICE = { "序号", "集中编码","产品供应商分类" ,"备注"};
    public boolean[] COL_CHECK = new boolean[4];
    public GSManufactureDAO dao;
    public GSProductTypeView bean = null;
	
	public String execute(GSBaseForm aForm) throws Exception {
        GSImportExcelForm tGSImportExcelForm = (GSImportExcelForm) aForm;
        FormFile formFile = tGSImportExcelForm.getExcelfile();
        tGSImportExcelForm.getRequest().setAttribute("target", "_self");
        Object modify = aForm.getSession().getAttribute("modify");

        tGSImportExcelForm.getRequest().setAttribute("action", "A400301.do");
        if (formFile!=null) {
            ArrayList tList = loadDataFromExcel(formFile.getInputStream());
            ResultProcess tProc = new ResultProcess();
            tProc.setResult(tList);
           
            aForm.getSession().setAttribute("manufactureExcel", tProc);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
	}
	
	  public ArrayList loadDataFromExcel(InputStream aInputStream) throws Exception {
		  dao = new GSManufactureDAO();
	        ArrayList tPageDataList = new ArrayList();
	        Workbook tExcelWorkbook = null;

	        try {
	            tExcelWorkbook = Workbook.getWorkbook(aInputStream);

	            Sheet tExcelSheet = tExcelWorkbook.getSheet(SHEET_NAME);
	            if (tExcelSheet == null) {
	                throw new GSException("批量导入产品失败:文件格式不正确,无法找到：" + SHEET_NAME);
	            }
	            for (int i = 1; i < tExcelSheet.getRows(); i++) {
	                Cell tCells[] = tExcelSheet.getRow(i);
	                String tRowText = "";
	                int num = tCells.length;
	                for (short j = 0; j < num; j++) {
	                    Cell tExcelCell = tCells[j];
	                    if (tExcelCell == null) {
	                        break;
	                    }
	                    String tCellValue = tExcelCell.getContents();
	                    if (tCellValue.length() == 0) {
	                        tCellValue = " ";
	                    }
	                    tRowText += tCellValue + SEP;
	                }
		            for (int a = num; a < HEAD_NAMES.length; a++) {
		                  tRowText += " " + SEP;
		            }
	                
	                tRowText = this.checkManufacture(tRowText,i-1);
	                for (int tJ = 0; tJ < COL_CHECK.length; tJ++) {
	                    if (COL_CHECK[tJ] == false) {
	                        throw new GSException(tRowText);
	                    }
	                }
	                
	                tPageDataList.add(bean);
	                GSLogWriter.info(tRowText);
	            }
	        } catch (BiffException e) {
	            GSLogWriter.error(e.getMessage());
	            throw new GSException("批量导入产品供应商分类失败:文件格式不正确！");
	        } catch (IOException e) {
	            GSLogWriter.error(e.getMessage());
	            throw new GSException("批量导入产品供应商分类失败:文件格式不正确！");
	        } finally {
	            if (tExcelWorkbook != null)
	                tExcelWorkbook.close();
	        }
	        return tPageDataList;
	    }
	  public String checkManufacture(String aRowText,int a) throws GSException {
	        for (int i = 0; i < COL_CHECK.length; i++) {
	        	COL_CHECK[i] = true;
	        }
		  	bean = new GSProductTypeView();
	        String[] tRecorderColumns = aRowText.split(SEP);
	        if(tRecorderColumns[1].trim().equals("")){
	        	COL_CHECK[1] = false;
	        	return HEAD_NAMES[0] + tRecorderColumns[0] + ": " + HEAD_NAMES[1] + "不能为空！";
	        }
	        GSManufactureDAO dao = new GSManufactureDAO(); 
	        GSManufactureBean mbean =  dao.getManufactureByConcentricityCode(tRecorderColumns[1], " select * from t_product ");
	        if(mbean == null){
	        	COL_CHECK[1] = false;
	        	return HEAD_NAMES[0] + tRecorderColumns[0] + ": " + HEAD_NAMES[1] + "产品不存在！";
	        }
	        if(mbean.getIs_buy().equals("0")||mbean.getGs_validity().equals("0")){
	        	COL_CHECK[1] = false;
	        	return HEAD_NAMES[0] + tRecorderColumns[0] + ": " + HEAD_NAMES[1] + "产品不可采购或者无效！";
	        }
	        if(tRecorderColumns[2].trim().equals("")){
	    	        	COL_CHECK[2] = false;
	    	        	return HEAD_NAMES[0] + tRecorderColumns[0] + ": " + HEAD_NAMES[2] + "不能为空！";
	    	        	
	    	        }
	        GSProductTypeManagerDao productTypeManagerDao = new GSProductTypeManagerDao();
	        GSProductTypeManagerBean productTypemanagerBean = productTypeManagerDao.findByTypeCodeAndPUpperCode(tRecorderColumns[2].trim(),String.valueOf(GSBaseConstants.PRODUCT_TYPE_MANAGER_USE));
	        
	        if(productTypemanagerBean==null){
	        	COL_CHECK[2] = false;
	        	return HEAD_NAMES[0] + tRecorderColumns[0] + ": " + HEAD_NAMES[2] + "产品使用分类不存在！";
	        	
	        }
	        bean.setProductCode(mbean.getGs_concentricity());
	        bean.setProductId(mbean.getGs_manufacture_id());
	        bean.setProductType(Integer.parseInt(tRecorderColumns[2]));
	        bean.setProviderCode(mbean.getGs_provider_id());
	        bean.setGs_recorder_id(String.valueOf(a));
	        bean.setMode(mbean.getGs_manufacture_type());
	        bean.setSpace(mbean.getGs_manufacture_spec());
	        bean.setPrice(String.valueOf(mbean.getGs_manufacture_unitprice()));
	        bean.setRemark(tRecorderColumns[3]);
	        bean.setProductName(mbean.getGs_manufacture_name());
	        GSProviderDAO providerDao = new GSProviderDAO();
	        GSProviderBean provider = providerDao.queryProvider(mbean.getGs_provider_id());
	        if(provider == null){
	        	bean.setProviderName("");
	        }else{
	        	bean.setProviderName(provider.getGs_provider_name());
	        }
	        
	        bean.setUnitPrive(mbean.getGs_quant_unit());
	        return aRowText + "匹配成功！";
	    }
}
